package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AssertUtil;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.user.UserSubscribe;
import com.atguigu.tingshu.user.mapper.UserSubscribeMapper;
import com.atguigu.tingshu.user.service.UserSubscribeService;
import com.atguigu.tingshu.vo.user.UserSubscribeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service()
public class UserSubscribeServiceImpl extends ServiceImpl<UserSubscribeMapper, UserSubscribe> implements UserSubscribeService {


    @Autowired
    private UserSubscribeMapper userSubscribeMapper;
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Override
    public Page<UserSubscribeVo> findUserSubscribePage(Long pageNum, Long pageSize) {
        // 分页查询订阅的基本信息
        Page<UserSubscribe> page = userSubscribeMapper.selectPage(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<UserSubscribe>()
                .eq(UserSubscribe::getUserId, AuthContextHolder.getUserId())
                .orderByDesc(UserSubscribe::getCreateTime));
        //
        List<UserSubscribe> userSubscribes = page.getRecords();
        List<UserSubscribeVo> userSubscribeVos = userSubscribes.stream().map(
                userSubscribe -> {
                    UserSubscribeVo userSubscribeVo = new UserSubscribeVo();
                    userSubscribeVo.setAlbumId(userSubscribe.getAlbumId());
                    userSubscribeVo.setCreateTime(userSubscribe.getCreateTime());

                    // 远程调用根据专辑id查询专辑的信息
                    Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfoById2(userSubscribe.getAlbumId());
                    AssertUtil.resultAssert(albumInfoResult, ResultCodeEnum.DATA_ERROR);
                    AlbumInfo albumInfo = albumInfoResult.getData();
                    userSubscribeVo.setAlbumTitle(albumInfo.getAlbumTitle());
                    userSubscribeVo.setCoverUrl(albumInfo.getCoverUrl());
                    userSubscribeVo.setIncludeTrackCount(albumInfo.getIncludeTrackCount());
                    // 判断专辑是否完结
                    userSubscribeVo.setIsFinished(albumInfo.getIsFinished().toString());
                    return userSubscribeVo;

                }


        ).toList();
        Page<UserSubscribeVo> userSubscribeVoPage = new Page<>(pageNum, pageSize, page.getTotal());
        userSubscribeVoPage.setRecords(userSubscribeVos);
        userSubscribeVoPage.setPages(page.getPages());
        userSubscribeVoPage.setCurrent(page.getCurrent());
        userSubscribeVoPage.setSize(page.getSize());
        userSubscribeVoPage.setTotal(page.getTotal());

        return userSubscribeVoPage;
    }
}
